package com.glorysoft.wangnianxinpda.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Context;
import android.os.Bundle;

import com.glorysoft.wangnianxinpda.R;
import com.glorysoft.wangnianxinpda.adapter.MaterialAdapter;
import com.glorysoft.wangnianxinpda.base.App;
import com.glorysoft.wangnianxinpda.base.BaseActivity;
import com.glorysoft.wangnianxinpda.base.Constant;
import com.glorysoft.wangnianxinpda.databinding.ActivityChangeShiftBinding;
import com.glorysoft.wangnianxinpda.entity.PDALotInfoReturnEntity;
import com.glorysoft.wangnianxinpda.entity.net.ResponseEntity;
import com.glorysoft.wangnianxinpda.utils.MyUtils;
import com.glorysoft.wangnianxinpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;

import java.util.ArrayList;

/**
 * 交接班检产量汇报
 */
public class ChangeShiftActivity extends BaseActivity {

    private ActivityChangeShiftBinding dataBinding;

    private Context mContext;
    private String sEqpId;
    private String sLotId;
    private String sGoodQty;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutResId() {
        return 0;
    }

    @Override
    protected void initView() {
        mContext = this;
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_change_shift);

    }

    @Override
    protected void initData() {
        Bundle extras = getIntent().getExtras();
        sEqpId = extras.getString("EQPID");
        dataBinding.setEqpId(sEqpId);
        MyUtils.setNewEditTextListener(dataBinding.etLot, mContext, s -> {
            sLotId = s;
        });

        MyUtils.setNewEditTextListener(dataBinding.etGoodQty, mContext, s -> {
            sGoodQty = s;
        });
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle(getString(R.string.changeShiftSubmit));
        freeUI_titleView.setRightListener(getString(R.string.Submit), v -> PDAChangeShift());
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), dataBinding.llMain);
    }

    //region 接口调用
    //交接班见产量汇报
    private void PDAChangeShift() {
        FreeUI_EntityCallBack<ResponseEntity<?>> callBack = new FreeUI_DialogEntityCallBack<ResponseEntity<?>>(new TypeToken<ResponseEntity<?>>() {
        }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseEntity<?>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(false, getSupportFragmentManager())) {
                    showMessageDialog(response.body().getMsg(), getSupportFragmentManager());
//                    finish();
                }
            }

            @Override
            public void onError(Response<ResponseEntity<?>> response) {
                super.onError(response);
                loadError(response.getException(), "PDAChangeShifts");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("WorkNO", Constant.loginEntity.getWORK_NO());//工号
        map.addProperty("EQPID", sEqpId);//设备号
        map.addProperty("LOTID", sLotId);//批次
        map.addProperty("COUNT", sGoodQty);//合格数

        String method ="PDAChangeShifts";
        NetUtils.requestPostNet("PDAChangeShiftsRequest", method, this, map, callBack);
    }
    //endregion


    //消息提示框
    protected void showMessageDialog(String message, FragmentManager fragmentManager) {
        FreeApi_DialogUtils.getConfirmDialog(
                App.app, "确定", message, this::finish
        ).show(fragmentManager, "DIALOG");
    }
}